<?php

/*
 * 支付宝接口通知文件
 * 不需要加IN_DISCUZ判断
 *
 */

define('DISABLEXSSCHECK', true);
require '../../../source/class/class_core.php';
$discuz = C::app();
$discuz->init();
if(!isset($_G['cache']['plugin'])) {
	loadcache('plugin');
}
if(!empty($_POST)) {
	$notify = $_POST;
	$location = FALSE;
} else if(!empty($_GET)) {
	$notify = $_GET;
	$location = TRUE;
} else {
	exit('Access Denied');
}
$ec_partner=C::t('#aljbd#aljbd_setting')->fetch('ec_partner');
if(dfsockopen("http://notify.alipay.com/trade/notify_query.do?partner=".$ec_partner['value']."&notify_id=".$notify['notify_id'], 60) !== 'true') {
	exit('Access Denied');
}
if ($_GET['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || $_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS'){
	$order = C::t('#aljbd#aljbd_order') -> fetch($notify['out_trade_no']);
	$good = C::t('#aljbd#aljbd_goods') -> fetch($order['sid']);
	if ($order['status'] == 1){
		$status = 2;
		C::t('#aljbd#aljbd_order')->update($notify['out_trade_no'], array('status' => $status, 'buyer' => "$notify[trade_no]", 'confirmdate' => $_G['timestamp']));
		C::t('#aljbd#aljbd_goods')->update_num2_by_id($good['id'],$order['amount']);

		notification_add($order['uid'], 'system',str_replace('{username}',$order['username'],str_replace('{shopname}','<a href="plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['suctips'])));
		$brand = C::t('#aljbd#aljbd') -> fetch($good['bid']);
		notification_add($good['uid'], 'system',str_replace('{orderid}','<a href="plugin.php?id=aljbd&act=orderlist">'.$notify['out_trade_no'].'</a>',str_replace('{username}',$brand['username'],str_replace('{name}',$order['username'],str_replace('{shopname}','<a href="plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['notify_merchant'])))));
		if($_G['cache']['plugin']['aljbd']['time']){
			$_G['siteurl'] = 'http://'.$_SERVER['HTTP_HOST'];
			$email_first = C::t("common_member")->fetch($order['uid']);
			$email = $email_first['email'];
			if($email_first['email']){
				$m=str_replace('{username}',$order['username'],str_replace('{shopname}','<a href="'.$_G['siteurl'].'/plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['suctips']));
				require_once libfile('function/mail');
				sendmail_cron($email,$_G['cache']['plugin']['aljbd']['mailtitle'].'@'.gmdate('Y-m-d H:i:s',$_G['timestamp']+8*3600),$m);
			}
			$email_f = C::t("common_member")->fetch($good['uid']);
			$e = $email_f['email'];
			if($email_f['email']){
				$me=str_replace('{orderid}','<a href="'.$_G['siteurl'].'/plugin.php?id=aljbd&act=orderlist">'.$notify['out_trade_no'].'</a>',str_replace('{username}',$brand['username'],str_replace('{name}',$order['username'],str_replace('{shopname}','<a href="'.$_G['siteurl'].'/plugin.php?id=aljbd&act=goodview&gid='.$good['id'].'">'.$good['name'].'</a>',$_G['cache']['plugin']['aljbd']['notify_merchant']))));
				$t=str_replace('{orderid}',$notify['out_trade_no'],$_G['cache']['plugin']['aljbd']['notify_merchant_title']);
				require_once libfile('function/mail');
				sendmail_cron($e,$t,$me);
			}
		}
	}
}

if($location){
	$_G['siteurl'] = 'http://'.$_SERVER['HTTP_HOST'];
	dheader('location: '.$_G['siteurl'].'/plugin.php?id=aljbd&act=orderlist');
}
?>